﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AssetManagementSystem.Model;
using AssetManagementSystem.Utils.Events;
using AssetManagementSystem.View.IView;
using AssetManagementSystem.Utils;

namespace AssetManagementSystem.Presenter {
    class AssetPresenter {

        AssetManager manager;
        IAssetView view;

        public AssetPresenter(IAssetView view) {
            manager = new AssetManager();
            this.view = view;
            //Handle Asset View Events
            this.view.Assets_LoadAll += new EventHandler(PresentAllAssets);
            this.view.Assets_Find += new EventHandler<SearchEventArgs>(PresentFoundAssets);
            this.view.Asset_Create += new EventHandler<AssetEventArgs>(CreateAsset);
            this.view.Asset_Update += new EventHandler<AssetEventArgs>(UpdateAsset);
            this.view.Asset_Delete += new EventHandler<AssetEventArgs>(DeleteAsset);
            this.view.AssetProperty_Delete += new EventHandler<AssetPropertiesEventArgs>(DeleteAssetProperties);
        }

        /// <summary>
        /// Provides a list of all assets to the view for display
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">event args</param>
        private void PresentAllAssets(object sender, EventArgs e) {
            view.SetAssetList(manager.GetAllAssets());
        }

        /// <summary>
        /// Provides a list of assets found based on search data provided in event args
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">event args, must contain search input</param>
        private void PresentFoundAssets(object sender, SearchEventArgs e) {
            view.SetAssetList(manager.FindAssets(e.SearchText));
        }

        /// <summary>
        /// Tries to create a new asset and refresh the view
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">args</param>
        private void CreateAsset(object sender, AssetEventArgs e) {
            try {
                //store the new asset
                manager.CreateAsset(e.Asset);
                //refresh the assets on the view
                PresentAllAssets(this, null);
                view.ShowMessage("Asset successfully saved.");
            } catch (Exception createFailed) {
                //if something goes wrong, send a message to the view
                view.ShowMessage(createFailed.Message);
            }
        }

        /// <summary>
        /// Tries to update an existing asset and refresh the view
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">args</param>
        private void UpdateAsset(object sender, AssetEventArgs e) {
            try {
                //update the asset
                manager.UpdateAsset(e.Asset);
                //refresh the assets on the view
                PresentAllAssets(this, null);
                view.ShowMessage("Asset successfully updated.");
            } catch (Exception updateFailed) {
                //if something goes wrong, send a message to the view
                view.ShowMessage(updateFailed.Message);
            }
        }

        /// <summary>
        /// Tries to delete an existing asset and refresh the view
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">args</param>
        private void DeleteAsset(object sender, AssetEventArgs e) {
            try {
                //delete the asset
                manager.DeleteAsset(e.Asset);
                //refresh the assets on the view
                PresentAllAssets(this, null);
                view.ShowMessage("Asset successfully deleted.");
            } catch (Exception deleteFailed) {
                //if something goes wrong, send a message to the view
                view.ShowMessage(deleteFailed.Message);
            }
        }

        /// <summary>
        /// Tries to delete all properties of an asset and refresh the view
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">args</param>
        private void DeleteAssetProperties(object sender, AssetPropertiesEventArgs e) {
            try {
                //delete the asset
                manager.DeleteAssetProperties(e.AssetId, e.Properties);
                //refresh the assets on the view
                PresentAllAssets(this, null);
            } catch (Exception deleteFailed) {
                //if something goes wrong, send a message to the view
                view.ShowMessage(deleteFailed.Message);
            }
        }

    }
}
